/*************************************************************************
	> File Name: ./include/PageCache.h
	> Author: zq
	> Mail: zhouquan1511@163.com 
	> Created Time: Thu 27 Apr 2023 10:49:29 PM CST
 ************************************************************************/
#pragma once

#include "Common.h"
#include <unordered_map>
#include "ObjectPool.h"

class PageCache {
	private:
		SpanList _spanLists[NPAGES];
		std::unordered_map<PAGE_ID, Span*> _idSpanMap; //Span管理多页，页号与Span的映射

	private:
		PageCache() {}
		PageCache(const PageCache&) {}
		
		static PageCache _sInst;
		ObjectPool<Span> _spanPool; //Span对象池，用于Span对象的内存分配

	public:
		std::mutex _pageMtx; //大锁

		static PageCache* GetInstance() {
			return &_sInst;
		}
		
		Span* NewSpan(size_t k); //在当前pageCache中找到并返回一个k页Span
		Span* MapObjectToSpan(void *obj); //获取从对象到Span的映射
		Span* ReleaseSpanToPageCache(Span *span); //释放Span到pageCache中
};
